/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.wbaseswt.action.base;

import org.eclipse.swt.widgets.Shell;

import top.wardyan.wbaseswt.conf.Const;

/**
 * 
 * @author ward 2019年3月9日
 */
public abstract class AbstractShell<T extends AbstractShell<T>> extends Shell{
	//protected Composite com;
	protected T self;
	
	public AbstractShell() {
		super();
		//setImage(SWTResourceManager.getImage(AbstractShell.class, "/conf/pics/umbrella.png"));
		this.initialize();
	}
	//@SuppressWarnings("unchecked")
	public AbstractShell(Integer style) {
		super(style);
		this.initialize();
	}
	
	@SuppressWarnings("unchecked")
	protected void initialize() {
		self = (T) this;
		this.setTitle();
		this.setBackground(Const.COLOR_WHITE);
		this.setImage(Const.ICON);
	}
	
	@Override
	public final void open(){
		super.open();
		this.layout();		
	}
	
	/**
	 * 这个是用来new窗口后,让它显示的
	 * 使用方法: new Window().go()
	 */
	public final void go(){
		this.open();
		while (!this.isDisposed()) {
			if (!this.getDisplay().readAndDispatch()) {
				this.getDisplay().sleep();
			}
		}
		if(isMainWindow())
			System.exit(0);
	}
	
	/**
	 * 如果返回true, 则关闭窗口时会导致应用程序关闭. 默认false
	 * @return
	 */
	protected boolean isMainWindow() {
		return false;
	}
	
	/** 就这样, 否则不能将子类作为父类来用 **/
	@Override
	protected final void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	public void setTitle() {
		this.setText(this.getTitle());
	}
	
	/**
	 * 设置系统标题. 推荐子类重写
	 */
	protected abstract String getTitle();


	
	
	
	/**
	public void switchMenu(Composite com) {
		if(this.com != null && !this.com.isDisposed()) {
			this.com.dispose();
			this.com = null;
		}
		this.com = com;
	}
	**/
}
